# -*- coding:utf-8 -*-

from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import xlrd


class Action():
    def __init__(self):
        # 初始化配置，设置Desired Capabilities参数
        self.desired_caps = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:62001",
            "appPackage": "com.sankuai.meituan.takeoutnew",
            "appActivity": "com.sankuai.waimai.business.page.homepage.MainActivity",
            "udid": "127.0.0.1:62001",
            "noReset": "True",
            "newCommandTimeout": "600",
            # "unicodeKeyboard": True,
            # "resetKeyboard": True

        }
        # 指定Appium Server
        self.server = 'http://localhost:4723/wd/hub'
        # 新建一个Session
        self.driver = webdriver.Remote(self.server, self.desired_caps)
        self.driver.implicitly_wait(10)
        time.sleep(5)
        #获取屏幕的高
        self.x = self.driver.get_window_size()['width']
        # 获取屏幕宽
        self.y = self.driver.get_window_size()['height']


    def get_content(self):
        # # 检查搜索框
        # print("---------点击【首页-二手房】------------")
        # self.driver.find_element_by_xpath('//*[contains(@text,"二手房")]').click()
        # print("---------点【二手房】------------")#com.wuba:id/recyclerView
        # WebDriverWait(self.driver, 5).until(
        #     EC.presence_of_element_located((By.ID, "com.wuba:id/iconSdv"))).click()
        # 向上滑动
        # print("---------【向上滑动】------------")
        # self.swipeUp(self.driver, t=700)
        while True:
            # 循环列表
            print("---------【获取列表中...】------------")
            userlist = WebDriverWait(self.driver, 10).until(
                    EC.presence_of_all_elements_located((By.ID, 'com.sankuai.meituan.takeoutnew:id/main_spu_info_container')))
            for u in userlist:
                try:
                    dict_data = dict()
                    # 标题
                    dict_data['title'] = u.find_element_by_id('com.sankuai.meituan.takeoutnew:id/txt_main_spu_name').get_attribute("text")
                    # 星级
                    # dict_data['rating_sales'] = u.find_element_by_id('com.sankuai.meituan.takeoutnew:id/rating_num').get_attribute("text")
                    # 月销售
                    dict_data['sales_tip'] = u.find_element_by_id('com.sankuai.meituan.takeoutnew:id/txt_month_sale').get_attribute("text")
                    # 配送时间 距离
                    # dict_data['distance_time'] = u.find_element_by_id('com.sankuai.meituan.takeoutnew:id/delivery_time').get_attribute("text")
                    # 起送价格
                    dict_data['price_info'] = u.find_element_by_id('com.sankuai.meituan.takeoutnew:id/txt_price').get_attribute("text").strip()
                    print(str(dict_data))
                    print("---------点【列表】------------")
                except:
                    pass

            print("---------【获取信息后，向上滑动】------------")
            self.swipeUp(self.driver, t=1000)




    '''
    参数 1：driver
    参数 2：t 是持续时间
    参数 3：滑动次数
    '''
    def swipeUp(self, driver, t=500, n=1):
        '''向上滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width'] *  0.5  # x 坐标
        y1 = l['height'] * 0.75 # 起始 y 坐标
        y2 = l['height'] * 0.25 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, t)

    def swipeDown(self, driver, t=500, n=1):
        '''向下滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.5 # x 坐标
        y1 = l['height'] * 0.25 # 起始 y 坐标
        y2 = l['height'] * 0.75 # 终点 y 坐标
        for i in range(n):
            driver.swipe(x1, y1, x1, y2, 1)


    def swipLeft(self, driver, t=500, n=1):
        '''向左滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.75
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.05
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)


    def swipRight(self, driver, t=500, n=1):
        '''向右滑动屏幕'''
        l = driver.get_window_size()
        x1 = l['width']  * 0.05
        y1 = l['height'] * 0.5
        x2 = l['width']  * 0.75
        for i in range(n):
            driver.swipe(x1, y1, x2, y1, t)



if __name__ == '__main__':
    action = Action()
    action.get_content()
